package bankapp.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import bankapp.BankSystem;
import bankapp.util.BankSystemConstants;
import frmsystem.gui.CompanyRegistrationGUI;
import frmsystem.gui.FrameworkController;
import frmsystem.gui.MainUI;
import frmsystem.gui.OperationPanel;
import frmsystem.gui.PersonalRegistrationGUI;
import frmsystem.gui.TransactionUI;

public class BankController extends FrameworkController {

	private BankTransactionPanel btPanel;
	
	
	public BankController(){
		domain = BankSystem.getInstance();
		
		operationPanel = new OperationPanel(580, 50);
		operationPanel.setAddCompanyAccountListener(new CompanyAccountListener());
		operationPanel.setAddPersonalAccountListener(new PersonalAccountListener());
		operationPanel.setAddInterestListener(new InterestListener());
		
		btPanel = new BankTransactionPanel(100, 380);
		btPanel.addTransactionListener(new WithdrawListener());
		btPanel.addDepositActionListener(new DepositListener());
		btPanel.addExitListener(new ExitListener());
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		mainUI = new MainUI(operationPanel, btPanel, table, 700, 700);
		
		mainUI.setVisible(true);
	}
	
	private void updateData(){
		
		DefaultTableModel model = new DefaultTableModel(domain.getAccountData(), domain.getTitles());
		table.setModel(model);
		table.repaint();
	}
	
	protected class PersonalAccountListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			personalGui = new PersonalRegistrationGUI();
			personalGui.addOkActionListener(new PersonalOkListener());
			personalGui.addCancelActionListener(new PersonalCancelListener());
			personalGui.setVisible(true);
		}
		
	}
	
	protected class CompanyAccountListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			companyGui = new CompanyRegistrationGUI();
			companyGui.addOkActionListener(new CompanyOkListener());
			companyGui.addCancelActionListener(new CompanyCancelListener());
			companyGui.setVisible(true);
		}
		
	}
	
	protected class InterestListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			domain.applyInterest();
			updateData();
		}
		
	}
	
	private class DepositListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 6);
				String partyname = (String)table.getValueAt(row, 0);
				transUI = new TransactionUI(val, partyname);
				transUI.addOkActionListener(new DepositOkListener());
				transUI.addCancelActionListener(new TransactionCancelListener());
				transUI.setVisible(true);
			}
			
		}
		
	}
	
	private class WithdrawListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 6);
				String partyname = (String)table.getValueAt(row, 0);
				transUI = new TransactionUI(val, partyname);
				transUI.addOkActionListener(new WithdrawOkListener());
				transUI.addCancelActionListener(new TransactionCancelListener());
				transUI.setVisible(true);
			}
			
		}
		
	}
	
	private class DepositOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String amt = transUI.getTxtAmount().getText();
			String accNO = transUI.getTxtAccNo().getText();
			String partyname = transUI.getParty();
			
			if(!"".equals(amt)){
				double am = Double.parseDouble(amt);
				domain.doTransaction(partyname,accNO, BankSystemConstants.TRANSACTION_DEPOSIT, am);
				updateData();
			}
			
			transUI.dispose();
		}
		
	}
	
	private class WithdrawOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String amt = transUI.getTxtAmount().getText();
			String accNO = transUI.getTxtAccNo().getText();
			String partyname = transUI.getParty();
			
			if(!"".equals(amt)){
				double am = Double.parseDouble(amt);
				domain.doTransaction(partyname, accNO, BankSystemConstants.TRANSACTION_WITHDRAW, am);
				updateData();
			}
			
			transUI.dispose();
		}
		
	}
	
	protected class ExitListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);			
		}
		
	}
	
	protected class PersonalOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int cardType;
			if(personalGui.getChkCheckings().isSelected()){
				cardType = BankSystemConstants.ACCOUNT_CHECKING;
			}else {
				cardType = BankSystemConstants.ACCOUNT_SAVINGS;
			}
			String name = personalGui.getTxtName().getText();
			String street = personalGui.getTxtStreet().getText();
			String city = personalGui.getTxtCity().getText();
			String state = personalGui.getTxtState().getText();
			String zip = personalGui.getTxtZip().getText();
			String email = personalGui.getTxtEmail().getText();
			String dob = personalGui.getTxtDOB().getText();
			personalGui.dispose();
			domain.registerPerson(name, street, city, state, zip, email, dob, cardType, null);
			updateData();
		}
		
	}
	
	protected class PersonalCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			personalGui.dispose();			
		}
		
	}
	
	protected class CompanyOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int cardType;
			if(companyGui.getChkCheckings().isSelected()){
				cardType = BankSystemConstants.ACCOUNT_CHECKING;
			}else {
				cardType = BankSystemConstants.ACCOUNT_SAVINGS;
			}
			String name = companyGui.getTxtName().getText();
			String street = companyGui.getTxtStreet().getText();
			String city = companyGui.getTxtCity().getText();
			String state = companyGui.getTxtState().getText();
			String zip = companyGui.getTxtZip().getText();
			String email = companyGui.getTxtEmail().getText();
			int noe = Integer.parseInt(companyGui.getTxtnoe().getText());
			companyGui.dispose();
			domain.registerOrganization(name, street, city, state, zip, email, noe, cardType, null);
			updateData();
		}
		
	}
	
	protected class CompanyCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			companyGui.dispose();			
		}
		
	}
	
	protected class TransactionCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			transUI.dispose();
			
		}
		
	}
	
	
}
